package com.ruoyi.web.controller.dr;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.dr.domain.DrOwnerUnion;
import com.ruoyi.dr.domain.DrSellAnalysis;
import io.netty.util.internal.ObjectUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dr.domain.DrSellDetails;
import com.ruoyi.dr.service.IDrSellDetailsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 销售明细Controller
 * 
 * @author ruoyi
 * @date 2025-07-14
 */
@RestController
@RequestMapping("/dr/sellDetails")
public class DrSellDetailsController extends BaseController
{
    @Autowired
    private IDrSellDetailsService drSellDetailsService;

    /**
     * 查询销售明细列表
     */
//    @PreAuthorize("@ss.hasPermi('system:details:list')")
    @GetMapping("/list")
    public TableDataInfo list(DrSellDetails drSellDetails)
    {
        startPage();
        List<DrSellDetails> list = drSellDetailsService.selectDrSellDetailsList(drSellDetails);
        return getDataTable(list);
    }

    /**
     * 导出销售明细列表
     */
//    @PreAuthorize("@ss.hasPermi('system:details:export')")
    @Log(title = "销售明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DrSellDetails drSellDetails)
    {
        List<DrSellDetails> list = drSellDetailsService.selectDrSellDetailsList(drSellDetails);
        ExcelUtil<DrSellDetails> util = new ExcelUtil<DrSellDetails>(DrSellDetails.class);
        util.exportExcel(response, list, "销售明细数据");
    }

    /**
     * 获取销售明细详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:details:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId)
    {
        return success(drSellDetailsService.selectDrSellDetailsByOrderId(orderId));
    }

    /**
     * 新增销售明细
     */
//    @PreAuthorize("@ss.hasPermi('system:details:add')")
    @Log(title = "销售明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DrSellDetails drSellDetails)
    {
        return toAjax(drSellDetailsService.insertDrSellDetails(drSellDetails));
    }

    /**
     * 修改销售明细
     */
//    @PreAuthorize("@ss.hasPermi('system:details:edit')")
    @Log(title = "销售明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DrSellDetails drSellDetails)
    {
        return toAjax(drSellDetailsService.updateDrSellDetails(drSellDetails));
    }

    /**
     * 删除销售明细
     */
//    @PreAuthorize("@ss.hasPermi('system:details:remove')")
    @Log(title = "销售明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable String[] orderIds)
    {
        return toAjax(drSellDetailsService.deleteDrSellDetailsByOrderIds(orderIds));
    }



    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<DrSellDetails> util = new ExcelUtil<>(DrSellDetails.class);
        util.importTemplateExcel(response, "销售详细数据");
    }

    @PostMapping("/select/count/aweme/product")
    public AjaxResult selectCountByAwemeProductOwner(DrSellAnalysis drSellAnalysis) {
        String username = getLoginUser().getUsername();
        if (!StringUtils.equals(username, "admin")) {
            drSellAnalysis.setOwner(username);
        }
        List<DrSellAnalysis> sellAnalysis = drSellDetailsService.selectCountByAwemeProductOwner(drSellAnalysis);
        return success(sellAnalysis);
    }

    @PostMapping("/selectDrSellAnalysisList")
    public AjaxResult selectDrSellAnalysisList(@RequestBody DrSellAnalysis drSellAnalysis) {
        String username = getLoginUser().getUsername();
        if (!StringUtils.equals(username, "admin")) {
            drSellAnalysis.setOwner(username);
        }
        List<DrSellAnalysis> sellAnalysis = drSellDetailsService.selectDrSellAnalysisList(drSellAnalysis);
        return success(sellAnalysis);
    }

    @Log(title = "寄样表导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importSellDetails(MultipartFile file) throws Exception {
        ExcelUtil<DrSellDetails> util = new ExcelUtil<>(DrSellDetails.class);
        List<DrSellDetails> sellDetails = util.importExcel(file.getInputStream());
        String message = drSellDetailsService.importSellDetails(sellDetails, getLoginUser().getUsername());
        return success(message);
    }
}
